package defectsim.resources;

import javax.vecmath.Vector2d;

import repast.simphony.parameter.Parameters;

import defectsim.SimulationEnvironment;
import defectsim.agents.DefectAgent;
import defectsim.agents.EdgeDislocation;

public class FrictionForce extends DefectResource implements Force {

	private Vector2d frictionStress;
	private ForceType fType;
	private double unitLength = 1.0;

	private SimulationEnvironment env = SimulationEnvironment.getInstance();

	private static FrictionForce _instance = null;

	static public FrictionForce getInstance() {
		if(_instance == null) {
			_instance = new FrictionForce();
		}
		return _instance;
	}
	
	// Friction force class is singleton
	private FrictionForce(){
		fType = ForceType.Friction;
	}
	
	public void initialize(){
		Parameters p = env.getParameters();
		double frcs = (Double)p.getValue("frictionstress");
		this.frictionStress = new Vector2d(frcs, 0);		
	}
	
	public Vector2d getFrictionStress() {
		return frictionStress;
	}

	public Vector2d compute(DefectAgent da) {
		double b = 0;
		if (da instanceof EdgeDislocation){
			EdgeDislocation d = (EdgeDislocation)da;
			b = d.getUnitBurgersVector() * env.getMaterial().getBurgersVector();
		}
		return new Vector2d(Math.abs(b * frictionStress.x) * unitLength, Math.abs(b * frictionStress.y));
	}
	
	@Override
	public ForceType getForceType() {
		return fType;
	}

	@Override
	public void update() {

	}

	@Override
	public Vector2d getStress(DefectAgent da) {
		return new Vector2d(Math.abs(frictionStress.x) * unitLength, Math.abs(frictionStress.y));
	}
}
